jsx3.require("jsx3.gui.Block");

jsx3.Class.defineClass(
        "ux.megatron.example.tour.TourDesktop",
        jsx3.gui.Block,
        [ux.megatron.plugin.PluginContainerInterface],
        function (desktop) {

            desktop.prototype.tabsToLoad = new Object();
            desktop.prototype.allTabs = new Object();
            desktop.prototype.tabbedPane = null;

            desktop.prototype.init = function(strName, vntLeft, vntTop, vntWidth, vntHeight, strHTML) {
                this.jsxsuper(strName, vntLeft, vntTop, vntWidth, vntHeight, strHTML);
            }

            desktop.prototype.initialize = function() {
                try {
                    ux.megatron.Main.setServer("APP.ux.megatron");
                    ux.megatron.Main.setContextRootForNamespace("ux.megatron.example.tour", "MegatronTour");
                }
                catch(ex) {
                    ux.megatron.Main.logException(ex);
                }

                this.tabbedPane = this.getDescendantOfName("blkPerspectiveDisplay_00");
                this.onReady();
                this.tabbedPane.subscribe(ux.megatron.gui.PerspectiveTabbedPane.TAB_SELECTED, this, this.loadTab);
            }

            desktop.prototype.addPreLoadClass = function(mainClass, subClass, preLoad) {
                var tour = ux.megatron.example.tour;
                if(!tour[mainClass] || !tour[mainClass][subClass]) {
                    try{
                        ux.megatron.Main.loadClass("ux/megatron/example/tour/"+mainClass+"/"+subClass+".js", "ux.megatron.example.tour");
                        preLoad.add(tour[mainClass][subClass], preLoad.size());
                    } catch(ex) {
                        ux.megatron.Main.logException(ex);
                    }
                } else {
                    preLoad.add(tour[mainClass][subClass], preLoad.size());
                }
            }

            desktop.prototype.onReady = function() {
                this.tempBlock = new jsx3.gui.Block();
                if (this.getPluginList().size() == 0) {
                    var preLoad = new jsx3.util.List(0);
                    this.addPreLoadClass("welcome" , "WelcomePage" , preLoad);
                    this.addPreLoadClass("dialog"  , "TestDialog"  , preLoad);
                    this.addPreLoadClass("resource", "TestResource", preLoad);
                    this.addPreLoadClass("panel"   , "TestPanel"   , preLoad);
                    this.addPreLoadClass("effect"  , "TestEffect"  , preLoad);
                    this.loadPlugins(preLoad);
                }
                else {
                    this.onAfterGetPlugins();
                }
            }

            /**
             * Called when the component should be destroyed.
             * @private
             */
            desktop.prototype.destroyMe = function() {
                var iter = this.getPluginList().iterator();
                while (iter.hasNext()) {
                    try {
                        iter.next().destroyPlugin();
                    }
                    catch(ex) {
                        ux.megatron.Main.logException(ex);
                    }
                }
                this.getPluginList().clear();
            }

            desktop.prototype.setupPlugin = function(obj) {
                obj.callbackTarget = this;
                obj.selectionCallback = this.stage2ItemSelected;
            }

            desktop.prototype.onAfterGetPlugins = function() {
                                    
                var iter = this.getPluginList().iterator();
                var first = true;
                var tabtext = null;
                var isloaded = true;
                while (iter.hasNext()) {
                    var obj = iter.next();
                    tabtext = obj.getTabText();
                    var tab = this.tabbedPane.addTab(tabtext); 
                    if (first) {
                        obj.loadComponent(tab.getContentPane());
                        first = false;
                        this.allTabs['tab' + tabtext] = obj;
                    }
                    else {
                        this.tabsToLoad['tab' + tabtext] = obj;
                        this.allTabs['tab' + tabtext] = obj;
                    }
                }
            }

            desktop.prototype.loadTab = function(objEvent) {
                var name = objEvent.target.getText();
                var obj = this.tabsToLoad['tab' + name];
                if (obj) {
                    obj.loadComponent(objEvent.target.getContentPane());
                    delete this.tabsToLoad['tab' + name];
                }
                else {
                    obj = this.allTabs['tab' + name];
                    if (obj && obj.tabSelected) {
                        setTimeout(function() {
                            obj.tabSelected(objEvent.target.getContentPane());
                        }, 1);
                    }
                }
            }

        });